<template>
  <div class='forumDiv'>
    <div style="border-bottom:  1px solid #ccc;">
      <div class="title"><i class="el-icon-edit"></i> 综合练习
        <span style="float: right;margin-right: 10px;">练习：{{count1}}   正确：{{count2}}</span>
      </div>
    </div>
    <div class="content">
      <span class="timu">{{item.title}}</span>
      <span class="tag" style="float: none">{{item.type|statusFilter}}</span>
      <div class="xuanxiang" v-for="option in item.options">
        <el-radio style="width: 70%" v-model="radio" :label="option.id" border size="medium">{{option.optionTitle}}
        </el-radio>
      </div>
      <div v-if="finish&&item.options.length >0" style="margin-top: 10px">
        正确答案：{{currentOption}}
      </div>
      <div class="jiaojuan">
        <el-button v-if="finish" class="el-icon-goods" v-on:click="getAss(currentId)">解析</el-button>
        <el-button v-if="!finish" type="danger" v-on:click="checkAnswer">确定</el-button>
        <el-button v-if="finish" type="success" v-on:click="getData(currentId)">下一道</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import {postRecord} from '../api/dati'

  export default {
    data() {
      return {
        radio: '',
        currentId: 0,
        currentOption: '',
        item: {
          title: '',
          type: 0,
          isRight: 0,
          options: []
        },
        count: 0,
        finish: false
      };
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'java',
          2: '数据库',
          3: 'HTML',
          4: 'python'
        }
        return statusMap[status]
      }
    },
    created() {
      this.checkoutUser()
      this.getData(0);
    },
    computed: {
      count1() {
        return this.$store.state.count;
      },
      count2() {
        return this.$store.state.right;
      }
    },
    methods: {
      checkoutUser() {
        var _self = this;
        let _username = this.$store.state.username;
        if (_username == '') {
          _self.$router.push('/login');
        }
      },
      getData(param) {
        var _self = this;
        let url = 'http://localhost:8999/api/item/' + param;
        axios.get(url)
          .then(function (response) {
            var _data = response.data;
            if (_data.ret == true) {
              _data = _data.data;
              _self.item.title = _data.title;
              _self.item.type = _data.type;
              var _options = _data.options;
              _self.item.options = _options;
              _self.currentId = _data.id;
              for (var option of _options) {
                if (option.isRight == 1) {
                  _self.item.isRight = option.id
                  _self.currentOption = option.optionTitle
                }
              }
              _self.finish = false;
            } else {
              _self.$message(_data.data)
            }
          }).catch(res => {
          throw new Error('MaiSec.vue: ', res)
        })
      },
      checkAnswer() {
        console.log("changdu" + (this.item.options.length == 0))
        if (!this.item.options.length == 0) {
          var _self = this;
          var _radio = this.radio;
          if (_radio == undefined || _radio == '') {
            this.$message("请选择正确的选项")
            return
          }
          //判断是否正确，然后保存数据
          let result = 0;
          if (_self.item.isRight == _radio) {
            result = 1;
          }
          postRecord(this, result);
          _self.$store.commit("increment");
          if (_self.item.isRight == _radio) {
            _self.$store.commit("increment2");
            this.$message({message: "回答正确", type: 'success'})
          } else {
            this.$message({message: '回答错误', type: 'error'})
          }
        }
        this.finish = true;
      },
      getAss(_id) {
        this.$message({message: "暂无解析", type: 'warning'})
      }
    }
  };
</script>

<style scoped>
  .el-pagination .el-pager .active {

    background-color: #6bd5f1 !important;
  }

  .jiaojuan {
    margin-top: 50px;
    text-align: right;
    margin-right: 50px;
  }

  .xuanxiang {
    margin-top: 10px;
  }

  .timu {
    display: block;
    margin-top: 10px;
    margin-bottom: 10px;
    height: 50px;
  }

  .content {
    margin-left: 10px;
  }

  .forumDiv {
    padding: 0;
    box-sizing: border-box;
    border: none;
    background-color: #ffffff;
    width: 60%;
    display: block;
    margin-left: 5%;
    margin-top: 3%;

  }

  .tag {
    width: 50px;
    color: #017e66;
    display: block;
    line-height: 23px;
    padding: 0 12px;
    float: none;
    background-color: rgba(1, 126, 102, 0.08);
  }

  .stuff > span {
    font-size: 14px;
  }

  .desc {
    font-size: 1rem;
    color: #a1a8b8;
  }

  .listLeftRecommend {
    background-color: #009a61;
    float: left;
    text-align: center;
    line-height: 50px;
    margin-left: 10px;
    margin-top: 20px;
    font-size: 14px;
    padding: 0 10px;
    color: white;
  }

  .tag {
    color: #017e66;
    margin-right: 10px;
    display: block;
    float: left;
    line-height: 23px;
    padding: 0 12px;
    background-color: rgba(1, 126, 102, 0.08);
  }

  .profile {
    padding: 1rem;
    background: #eff2f7;
    border-radius: 0.3rem;
  }

  .profile div {
    margin-bottom: 1.5rem;
  }

  .profile div span {
    font-size: 25px;
    color: black;
    margin-left: 1rem;
  }

  .profile p {
    display: flex;
    align-items: center;
    color: grey;
    margin: 0.5rem 0;
  }

  .profile p span {
    margin: 0 0.5rem;
    color: black;
  }

  .profile svg {
    color: black;
    margin-left: 0.2rem;
  }

  .recentTopics img,
  .recentReplies img {
    width: 5rem;
    height: 5rem;
  }

  .recentTopics > p,
  .recentReplies > p {
    margin-bottom: 1rem;
    color: black;
    padding-bottom: 1.5rem;
  }

  .userTitle {
    font-size: 25px;
  }

  .recentTopics {
    padding: 1rem;
    background: #d3dce6;
    border-radius: 0.3rem;
  }

  .recentReplies {
    background: #e5e9f2;
    padding: 1rem;
    margin: 1rem 0;
    border-radius: 0.3rem;
  }

  .recentReplies div,
  .recentTopics div {
    display: flex;
    align-items: center;
    margin: 1rem 0;
    border-bottom: 2px solid #c0ccda;
    padding-bottom: 1rem;
  }

  .recentTopics div {
    border-bottom: 2px solid #99a9bf;
  }

  .recentReplies div img,
  .recentTopics div img {
    margin-right: 1rem;
  }

  .pagination {
    margin-top: 20px;
  }

  .el-pagination .el-pager .active {

    background-color: #6bd5f1
  }
</style>
